class AccountOperations:
    def __init__(self, db, card_id):
        self.db = db
        self.card_id = card_id

    def check_balance(self):
        query = "SELECT balance FROM cardinfo WHERE cardID = %s"
        result = self.db.execute_query(query, (self.card_id,))
        return result[0]['balance'] if result else 0.0

    def change_password(self, new_password):
        query = "UPDATE cardinfo SET pass = %s WHERE cardID = %s"
        self.db.execute_query(query, (new_password, self.card_id))
        print("密码修改成功")

    def report_loss(self):
        # 调用存储过程实现挂失
        result = self.db.execute_procedure("UpdateCardStatus", (self.card_id, "挂失", ""))
        print("挂失操作成功") if result else print("挂失操作失败")

    def cancel_loss_report(self):
        # 调用存储过程实现解挂
        result = self.db.execute_procedure("UpdateCardStatus", (self.card_id, "解挂", ""))
        print("解挂操作成功") if result else print("解挂操作失败")

    def get_transactions(self, start_date=None, end_date=None):
        if start_date and end_date:
            query = """
            SELECT * FROM transactionlog 
            WHERE customerID = (SELECT customerID FROM cardinfo WHERE cardID = %s)
            AND operationTime BETWEEN %s AND %s
            ORDER BY operationTime DESC
            """
            return self.db.execute_query(query, (self.card_id, start_date, end_date))
        else:
            query = """
            SELECT * FROM transactionlog 
            WHERE customerID = (SELECT customerID FROM cardinfo WHERE cardID = %s)
            ORDER BY operationTime DESC
            """
            return self.db.execute_query(query, (self.card_id,))